<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>红绿灯交替</title>
</head>
<body>
    <script>
        function red() {
            console.log('red');
        }

        function green() {
            console.log('green');
        }

        function yellow() {
            console.log('yellow');
        }

        // 模拟等待的函数
        function sleep(time) {
            return new Promise(resolve => setTimeout(resolve, time));
        }

        async function loop() {
            while (true) {
                await changelight(red, 3000);
                await changelight(green, 2000);
                await changelight(yellow, 1000);
            }
        }

        function changelight(light, time) {
            return new Promise((resolve) => {
                light();
                setTimeout(() => {
                    resolve();
                }, time);
            });
        }

        loop();
    </script>
</body>

</html>